package middleware

import (
	"errors"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"renting-demo/api/internal/config"
	"renting-demo/common"
)

type AuthTokenMiddleware struct {
	config.Config
}

func NewAuthTokenMiddleware() *AuthTokenMiddleware {
	return &AuthTokenMiddleware{}
}

func (m *AuthTokenMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation

		//todo:接受请求头部信息,鉴权逻辑处理
		tokenString := r.Header.Get("token")
		userID := r.FormValue("token")
		err := common.CheckJwtToken(tokenString, userID)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, errors.New("失败"))
		}
		next(w, r)

	}
}
